<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
			<el-form-item label="主键" prop="id">
				<el-input v-model="queryParams.id" placeholder="请输入主键" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="手机号" prop="phone">
				<el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="openid" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入openid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
			<el-form-item label="设备编码" prop="deviceCode">
				<el-input v-model="queryParams.deviceCode" placeholder="请输入设备编码" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="名称" prop="name">
				<el-input v-model="queryParams.name" placeholder="请输入名称" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<!-- <el-form-item label="部门id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入部门id"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
			<el-form-item label="性别" prop="sex">
				<el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
					<el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel"
						:value="dict.dictValue" />
				</el-select>
			</el-form-item>
			<!-- <el-form-item label="照片" prop="picture">
        <el-input
          v-model="queryParams.picture"
          placeholder="请输入照片"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
			<!-- <el-form-item label="身份证号" prop="personNumber">
        <el-input
          v-model="queryParams.personNumber"
          placeholder="请输入身份证号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
			<!-- <el-form-item label="职位" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入职位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
			<el-form-item label="座机" prop="telephone">
				<el-input v-model="queryParams.telephone" placeholder="请输入座机" clearable size="small"
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
					v-hasPermi="['manager:appUser:add']">新增</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
					v-hasPermi="['manager:appUser:edit']">修改</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
					v-hasPermi="['manager:appUser:remove']">删除</el-button>
			</el-col>
			<el-col :span="1.5">
				<el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport"
					v-hasPermi="['manager:appUser:export']">导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="appUserList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="主键" align="center" prop="id" />
			<el-table-column label="手机号" align="center" prop="phone" />
			<!-- <el-table-column label="openid" align="center" prop="openid" /> -->
			<el-table-column label="设备编码" align="center" prop="deviceCode" />
			<el-table-column label="名称" align="left" prop="name" />
			<el-table-column label="角色" align="left" prop="appRole" :formatter="roleFormat" />
			<!-- <el-table-column label="部门id" align="center" prop="deptId" /> -->
			<el-table-column label="性别" align="center" prop="sex" :formatter="sexFormat" />
			<!-- <el-table-column label="照片" align="center" prop="picture" /> -->
			<!-- <el-table-column label="身份证号" align="center" prop="personNumber" /> -->
			<el-table-column label="职位" align="center" prop="position" />
			<el-table-column label="座机" align="center" prop="telephone" />
			<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
						v-hasPermi="['manager:appUser:edit']">修改</el-button>
					<el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
						v-hasPermi="['manager:appUser:remove']">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize" @pagination="getList" />

		<!-- 添加或修改app用户对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="手机号" prop="phone">
					<el-input v-model="form.phone" placeholder="请输入手机号" />
				</el-form-item>
				<!-- <el-form-item label="openid" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item> -->
				<el-form-item label="设备编码" prop="deviceCode">
					<el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
				</el-form-item>
				<el-form-item label="名称" prop="name">
					<el-input v-model="form.name" placeholder="请输入名称" />
				</el-form-item>
				<el-form-item label="角色" prop="appRole">
					<el-select v-model="form.appRole" placeholder="请选择角色" style="width:100%">
						<el-option v-for="dict in role" :key="dict.dictValue" :label="dict.dictLabel"
							:value="parseInt(dict.dictValue)"></el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入部门id" />
        </el-form-item> -->
				<el-form-item label="性别" prop="sex">
					<el-select v-model="form.sex" placeholder="请选择性别" style="width:100%">
						<el-option v-for="dict in sexOptions" :key="dict.dictValue" :label="dict.dictLabel"
							:value="parseInt(dict.dictValue)"></el-option>
					</el-select>
				</el-form-item>
				<!-- <el-form-item label="照片" prop="picture">
          <el-input v-model="form.picture" placeholder="请输入照片" />
        </el-form-item> -->
				<!-- <el-form-item label="身份证号" prop="personNumber">
          <el-input v-model="form.personNumber" placeholder="请输入身份证号" />
        </el-form-item> -->
				<el-form-item label="职位" prop="position">
					<el-input v-model="form.position" placeholder="请输入职位" />
				</el-form-item>
				<el-form-item label="座机" prop="telephone">
					<el-input v-model="form.telephone" placeholder="请输入座机" />
				</el-form-item>
				<el-form-item label="上传头像" prop="telephone">
					<el-upload class="avatar-uploader" :action="uploadImgUrl" :show-file-list="false"
						:on-success="handleUploadSuccess" :headers="headers">
						<img v-if="imageUrl" :src="imageUrl" class="avatar">
						<i v-else class="el-icon-plus avatar-uploader-icon"></i>
					</el-upload>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>
<style>
	.avatar-uploader .el-upload {
		border: 1px dashed #d9d9d9;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}

	.avatar-uploader .el-upload:hover {
		border-color: #409EFF;
	}

	.avatar-uploader-icon {
		font-size: 28px;
		color: #8c939d;
		width: 178px;
		height: 178px;
		line-height: 178px;
		text-align: center;
	}
		
	.avatar {
		width: 178px;
		height: 178px;
		display: block;
	}
</style>
<script>
	import {
		listAppUser,
		getAppUser,
		delAppUser,
		addAppUser,
		updateAppUser,
		exportAppUser
	} from '@/api/person/appUser';
	import {
		getToken
	} from '@/utils/auth';
	export default {
		name: 'AppUser',
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// app用户表格数据
				appUserList: [],
				// 弹出层标题
				title: '',
				// 是否显示弹出层
				open: false,
				// 性别字典
				sexOptions: [],
				role: [],
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					id: null,
					phone: null,
					openid: null,
					deviceCode: null,
					name: null,
					deptId: null,
					sex: null,
					picture: null,
					personNumber: null,
					position: null,
					telephone: null
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				imageUrl: '',
				// 图片上传配置
				uploadImgUrl: process.env.VUE_APP_BASE_API + '/file/common/upload', // 上传的图片服务器地址
				headers: {
					Authorization: 'Bearer ' + getToken()
				},
				imgurl: process.env.VUE_APP_BASE_IMG,
				uploadshow: true,
			};
		},
		created() {
			this.getList();
			this.getDicts('sys_user_sex').then(response => {
				this.sexOptions = response.data;
			});
		},
		methods: {
			/** 查询app用户列表 */
			getList() {
				this.loading = true;
				listAppUser(this.queryParams).then(response => {
					this.appUserList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
				this.getDicts('app_role').then(response => {
					this.role = response.data;
				});
			},
			// 上传成功
			handleUploadSuccess(res) {
				console.log(res.url)
				this.form.picture = res.fileName;
				this.imageUrl = this.imgurl + res.url;
			},
			// 性别字典翻译
			sexFormat(row, column) {
				return this.selectDictLabel(this.sexOptions, row.sex);
			},
			roleFormat(row, column) {
				return this.selectDictLabel(this.role, row.appRole);
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					phone: null,
					openid: null,
					deviceCode: null,
					name: null,
					deptId: null,
					sex: null,
					picture: null,
					personNumber: null,
					position: null,
					telephone: null,
					createTime: null
				};
				this.imageUrl = '';
				this.resetForm('form');
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm('queryForm');
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id);
				this.single = selection.length !== 1;
				this.multiple = !selection.length;
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = '添加app用户';
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids;
				getAppUser(id).then(response => {
					this.form = response.data;
					this.imageUrl = process.env.VUE_APP_BASE_IMG + response.data.picture;
					this.open = true;
					this.title = '修改app用户';
				});
			},
			/** 提交按钮 */
			submitForm() {
				console.log(this.form)
				this.$refs['form'].validate(valid => {
				  if (valid) {
				    if (this.form.id != null) {
				      updateAppUser(this.form).then(response => {
				        this.msgSuccess('修改成功');
				        this.open = false;
				        this.getList();
				      });
				    } else {
				      addAppUser(this.form).then(response => {
				        this.msgSuccess('新增成功');
				        this.open = false;
				        this.getList();
				      });
				    }
				  }
				});
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$confirm('是否确认删除app用户编号为"' + ids + '"的数据项?', '警告', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(function() {
						return delAppUser(ids);
					})
					.then(() => {
						this.getList();
						this.msgSuccess('删除成功');
					});
			},
			/** 导出按钮操作 */
			handleExport() {
				const queryParams = this.queryParams;
				this.$confirm('是否确认导出所有app用户数据项?', '警告', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(function() {
						return exportAppUser(queryParams);
					})
					.then(response => {
						this.download(response.msg);
					});
			}
		}
	};
</script>
